package com.sunday.pinxinghui.base;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.util.Log;


import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.igexin.sdk.PushManager;
import com.sunday.common.event.EventBus;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.pinxinghui.entity.Member;
import com.sunday.pinxinghui.entity.Province;
import com.sunday.pinxinghui.event.LocSucceed;
import com.sunday.pinxinghui.push.PushIntentService;
import com.sunday.pinxinghui.push.PushService;
import com.sunday.pinxinghui.service.LocationService;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;

/**
 * Created by 刘涛 on 2017/8/15.
 */

public class BaseApplication extends Application implements Thread.UncaughtExceptionHandler {
    private static BaseApplication instance;

    public synchronized static BaseApplication getInstance() {
        return instance;
    }


    private Member member;
    private String cityName;
    private int cityId;

    public LocationService locationService;
    private LocationClient mClient;
    private Boolean isCanUpload = true;
    private boolean isLogin;
    protected  static final String TAG = "BaseApplication";
    private boolean isLocatioSuc;
    private String lati,longti;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;

        if (getCurProcessName(this) != null && getCurProcessName(this).equals("com.sunday.pinxinghui")) {
            /***
             * 初始化定位sdk，建议在Application中创建
             */
            UMShareAPI.get(getApplicationContext());
            locationService = new LocationService(getApplicationContext());
            Thread.setDefaultUncaughtExceptionHandler(this);
            PlatformConfig.setWeixin(Constant.APP_ID, Constant.APPSecret);
            Config.DEBUG = true;
            Config.isJumptoAppStore = true;

        }

    }

    public LocationService getLocationService(){
        return locationService;
    }
    public void requestLocation() {
        LocationClientOption mOption = locationService.getDefaultLocationClientOption();
        mOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        mOption.setCoorType("bd09ll");
        mClient = new LocationClient(getApplicationContext());
        mClient.setLocOption(mOption);
        mClient.registerLocationListener(listener);
        mClient.start();
    }
    /***
     * 定位结果回调，在此方法中处理定位结果
     */
    BDLocationListener listener = new BDLocationListener() {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // TODO Auto-generated method stub

            if (location != null && location.getLocType() != BDLocation.TypeServerError) {
                Log.i(TAG, "定位成功");
                Log.i(TAG, "currentThread" + Thread.currentThread());
                mClient.stop();
                String latitude = String.valueOf(location.getLatitude());
                String longitude = String.valueOf(location.getLongitude());
                SharePerferenceUtils.getIns(getApplicationContext()).putString("latitude", latitude);
                SharePerferenceUtils.getIns(getApplicationContext()).putString("longitude", longitude);
                BaseApplication.getInstance().setCityName(location.getCity());
                isLocatioSuc=true;
            } else {
                isLocatioSuc=false;
                ToastUtils.showToast(getApplicationContext(), "定位失败，请开启位置服务后重新定位");
            }
        }

        @Override
        public void onConnectHotSpotMessage(String s, int i) {

        }
    };


    private String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }


    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
        e.printStackTrace();
        System.exit(0);
    }


    public boolean isLogin() {
        return isLogin;
    }

    public void setLogin(boolean login) {
        isLogin = login;
    }


    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getCityName() {
        return cityName;
    }

    public int getCityId() {
        return cityId;
    }

    public void setCityId(int cityId) {
        this.cityId = cityId;
    }

    public String getLongti() {
        return longti;
    }

    public void setLongti(String longti) {
        this.longti = longti;
    }

    public String getLati() {
        return lati;
    }

    public void setLati(String lati) {
        this.lati = lati;
    }
}
